package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.OrderBean;
import com.itheima.entity.Result;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("orderMobile")
public class OrderMobileController {
    @Reference
    private OrderService orderService;
    @Autowired
    private JedisPool jedisPool;
    @RequestMapping("submitOrder")
    public Result submitOrder(@RequestBody OrderBean orderBean){
        String telephone = orderBean.getTelephone();
        Jedis jedis = jedisPool.getResource();
        String codeInRedis = null;
        try {
            codeInRedis = jedis.get("order_" + telephone);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(MessageConstant.VALIDATECODE_ERROR);
        } finally {
            jedis.close();
        }
        String validateCode = orderBean.getValidateCode();

        //校验手机验证码
        if(codeInRedis == null || !codeInRedis.equals(validateCode)){
            return Result.fail(MessageConstant.VALIDATECODE_ERROR);
        }
        orderBean.setOrderType("微信预约");
        Result result = orderService.submitOrder(orderBean);

        //发送预约成功通知

        return result;
    }
    @RequestMapping("findOrderInfoById")
    public Result findOrderInfoById(Integer id){
        return orderService.findById(id);
    }

}
